package com.edu.exam.entity.assessment.plan;

import com.edu.exam.enums.assessment.ChangeType;
import com.edu.exam.enums.assessment.generation.ReviewStatus;
import com.edu.exam.enums.common.OperationType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 考试计划版本实体类
 * 所属模块：B-05 考核计划管理模块
 * 对应数据库表：exam_plan_version
 *
 * <p>考试计划版本实体，记录计划变更的完整历史。
 * 每次计划的重要变更都会生成新版本，包含变更内容、
 * 变更原因、操作人等信息，支持版本对比和回滚。</p>
 *
 * @author 系统生成
 * @version 2.0
 * @since 2025-11-09
 */
@Entity
@Table(name = "exam_plan_version")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExamPlanVersion {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 考试计划ID
     */
    @Column(name = "plan_id", nullable = false)
    private Long planId;

    /**
     * 版本号
     */
    @Column(name = "version_number", length = 20, nullable = false)
    private String versionNumber;

    /**
     * 版本内容（JSON格式）
     */
    @Column(name = "content", columnDefinition = "TEXT", nullable = false)
    private String content;

    /**
     * 变更描述
     */
    @Column(name = "change_description", length = 1000)
    private String changeDescription;

    /**
     * 变更类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "change_type", length = 20, nullable = false)
    private ChangeType changeType;

    /**
     * 版本时间
     */
    @CreatedDate
    @Column(name = "version_time", nullable = false, updatable = false)
    private LocalDateTime versionTime;

    /**
     * 操作人ID
     */
    @Column(name = "operator_id", nullable = false)
    private Long operatorId;

    /**
     * 操作人姓名
     */
    @Column(name = "operator_name", length = 100)
    private String operatorName;

    /**
     * 操作类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_type", length = 20, nullable = false)
    private OperationType operationType;

    /**
     * 前一个版本ID
     */
    @Column(name = "previous_version_id")
    private Long previousVersionId;

    /**
     * 变更字段列表（JSON格式）
     */
    @Column(name = "changed_fields", columnDefinition = "TEXT")
    private String changedFields;

    /**
     * 变更原因
     */
    @Column(name = "change_reason", length = 1000)
    private String changeReason;

    /**
     * 审核状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "review_status", length = 20)
    private ReviewStatus reviewStatus;

    /**
     * 审核人ID
     */
    @Column(name = "reviewed_by")
    private Long reviewedBy;

    /**
     * 审核人姓名
     */
    @Column(name = "reviewed_by_name", length = 100)
    private String reviewedByName;

    /**
     * 审核时间
     */
    @Column(name = "reviewed_time")
    private LocalDateTime reviewedTime;

    /**
     * 审核意见
     */
    @Column(name = "review_comment", length = 1000)
    private String reviewComment;

    /**
     * 版本标签
     */
    @Column(name = "version_tag", length = 100)
    private String versionTag;

    /**
     * 是否为主要版本
     */
    @Column(name = "is_major", nullable = false)
    private Boolean isMajor;

    /**
     * 是否为稳定版本
     */
    @Column(name = "is_stable")
    private Boolean isStable;

    /**
     * 是否为当前版本
     */
    @Column(name = "is_current")
    private Boolean isCurrent;

    /**
     * 版本备注
     */
    @Column(name = "version_notes", length = 2000)
    private String versionNotes;

    /**
     * 附件路径
     */
    @Column(name = "attachment_path", length = 500)
    private String attachmentPath;

    /**
     * 删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 考试计划关联
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "plan_id", insertable = false, updatable = false)
    private ExamPlan examPlan;

    /**
     * 前一个版本
     */
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "previous_version_id", insertable = false, updatable = false)
    private ExamPlanVersion previousVersion;

}
